﻿using CloudMusic.Helper;
using CloudMusic.Model;
using CloudMusic.ViewModel;
using Flurl;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;

namespace CloudMusic.View
{
    /// <summary>
    /// PageSongSheetDetail.xaml 的交互逻辑
    /// </summary>
    public partial class PageSongSheetDetail : Page
    {

        #region 分页定义
        private long songId = 0;
        /// <summary>
        /// 收藏者信息
        /// </summary>
        public ObservableCollection<storeModel> StoreList { get; set; }
        #endregion 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pssvm"></param>
        /// <param name="id"></param>
        public PageSongSheetDetail(PageSongSheetDetailViewModel pssvm, long id,int hq)
        {
            InitializeComponent();
            this.DataContext = pssvm;
            this.SizeChanged += PageSongSheetDetail_SizeChanged;
            this.ItemIist.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.ItemIist_LoadingRow);
            switch (hq)
            {
                case 1:
                    sheetNameBorder.BorderBrush = new SolidColorBrush(Colors.Red);
                    sheetNameType.Text = "歌单";
                    sheetNameType.Foreground = new SolidColorBrush(Colors.Red);
                    break;
                case 2:
                    sheetNameBorder.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DE922F"));
                    sheetNameType.Text = "精品歌单";
                    sheetNameType.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DE922F"));
                    break;
                default:
                    break;
            }
            songId = id;
            GetComment(id,0);
            //this.itemsNum.ItemsSource = _collection;
        }
        /// <summary>
        /// 获取收藏者数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="offset"></param>
        public void GetComment(long id, int offset)
        {
            try
            {
                if (id == 0) return;
                this.storeUser.Dispatcher.BeginInvoke((Action)delegate ()
                {
                    var url = "";
                    if (PlayListHelper.UserInfos.Count > 0)
                    {
                        if (PlayListHelper.UserInfos[0].Cookie != "")
                        {
                            url = "http://localhost:3000/playlist/subscribers?id=" + id + "&limit=60&offset=" + offset + "&cookie=" + PlayListHelper.UserInfos[0].Cookie;

                        }
                    }
                    else
                    {
                        url = "http://localhost:3000/playlist/subscribers?id=" + id + "&limit=60&offset=" + offset + "&cookie="+"";
                    }
                    var strJosn = CommonHelper.Get(url);
                    storeUserModel sb = JsonConvert.DeserializeObject<storeUserModel>(strJosn);
                    List<storeModel> st = new List<storeModel>();
                    for (int i = 0; i < sb.subscribers.Count; i++)
                    {
                        storeModel sbitem = new storeModel
                        {
                            userId = sb.subscribers[i].userId,
                            avatarUrl = sb.subscribers[i].avatarUrl,
                            nickname = sb.subscribers[i].nickname
                        };
                        st.Add(sbitem);
                    }
                    storeUser.ItemsSource = st;
                });
            }
            catch (Exception) { };
        }
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            MusicViewModelBase.songListEvent += MusicViewModelBase_songListEvent;
            MusicViewModelBase.likesState += MusicViewModelBase_likesState;

            #region 收藏者分页数
            this.paging.PageCount = 1;
            this.paging.Page = 1;
            this.paging.RecordCount = 0;
            this.paging.PageSize = 60;
            this.paging.PageChanged += Paging_PageChanged;
            #endregion

        }

     

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Paging_PageChanged(object sender, UC.PageChangedEventArgs e)
        {
            sl.ScrollToVerticalOffset(0);
            GetComment(songId, (e.Page-1)*60);
        }
        /// <summary>
        /// 事件内容处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MusicViewModelBase_songListEvent(object sender, EventArgs e)
        {
            long id = (long)sender;
            var item = ObjectToEntityHelper.ConvertObjectByJson<PlayModel>((ObservableCollection<MusicModel>)ItemIist.ItemsSource);
            if (!PlayListHelper.MainMusicList.Any(t => t.Id == id))
            {
                item.ForEach(p => PlayListHelper.MainMusicList.Add(p));
            }
        }
        /// <summary>
        /// 歌曲喜欢状态修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MusicViewModelBase_likesState(object sender, EventArgs e)
        {
            long id = (long)sender;
            var item = (ObservableCollection<MusicModel>)ItemIist.ItemsSource;
            if (item.Any(t => t.Id == id))
            {
                var index = item.IndexOf(item.FirstOrDefault(t => t.Id == id));
                if (item[index].Like == 0)
                {
                    item[index].Like = 1;
                }
                else
                {
                    item[index].Like = 0;
                }
           
            }
        }
        /// <summary>
        /// 视图宽度变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageSongSheetDetail_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var wd = this.DataContext as PageSongSheetDetailViewModel;
            if (this.ActualWidth > 822)
            {
                wd.Width = 200;
                return;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridEquipment_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
        /// <summary>
        /// 滑动条关联
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            CommonHelper.RunScorllView(sender, e);
        }
        /// <summary>
        /// 添加到喜欢
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Like_Click(object sender, RoutedEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            long songId = music.Id;

        }
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Down_Click(object sender, RoutedEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            long songId = music.Id;
        }
        /// <summary>
        /// 跳转到专辑详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AlubmDetail_GotFocus(object sender, MouseButtonEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            var ps = new PageAlbumInfoViewModel(music.AlbumId);
            PageAlbumInfo pageAlbumInfo = new PageAlbumInfo(ps);
            NavigationService.Navigate(pageAlbumInfo);
        }
        /// <summary>
        /// 跳转到歌手详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SingerDetail_GotFocus(object sender, MouseButtonEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            long ablumId = music.SingerId;
            var ps = new PageSingerInfoViewModel(ablumId);
            PageSingerInfo pageSingerInfo = new PageSingerInfo(ps);
            NavigationService.Navigate(pageSingerInfo);
        }
        /// <summary>
        /// 生成序号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemIist_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            if (e.Row.GetIndex() < 9)
            {
                e.Row.Header = "0" + (e.Row.GetIndex() + 1);
            }
            else
            {
                e.Row.Header = e.Row.GetIndex() + 1;
            }
        }
        /// <summary>
        /// 分享页面开关
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShareBtn_Click(object sender, RoutedEventArgs e)
        {
            SharePop.IsOpen = true;
            e.Handled = false;
        }
        /// <summary>
        /// 双击选择行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemIist_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MusicModel music = (MusicModel)ItemIist.SelectedValue;
            var item = ObjectToEntityHelper.ConvertObjectByJson<PlayModel>((ObservableCollection<MusicModel>)ItemIist.ItemsSource);
            if (!PlayListHelper.MainMusicList.Any(t => t.Id == music.Id))
            {
                PlayListHelper.MainMusicList.Clear();
                item.ForEach(p => PlayListHelper.MainMusicList.Add(p));
            }

        }
        /// <summary>
        /// 滚动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            CommonHelper.RunScorllView(sender, e);
        }
        /// <summary>
        /// 跳转到收藏者信息页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void storeUser_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = storeUser.SelectedIndex;
            storeUser.SelectedIndex = i;
            if (storeUser.SelectedIndex != -1)
            {
                storeModel o = (storeModel)storeUser.SelectedItem;
                if (o == null) return;
                PageUserInfo pageUserInfo = new PageUserInfo();
                NavigationService.Navigate(pageUserInfo);
            }
            if (storeUser.SelectedIndex == -1) return;
            storeUser.SelectedIndex = -1;
        }
        /// <summary>
        ///进来获取收藏者数量分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void stor_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var rc = DataContext as PageSongSheetDetailViewModel;
            this.paging.RecordCount = rc.CollectionCount;
            //_collection.Clear();
            sl.ScrollToVerticalOffset(0);
        }

        private void col_Click(object sender, RoutedEventArgs e)
        {
            /// playlist / subscribe ? t = 1 & id = 106697785
           // songId
        }
    }
}
